<template>
  <!-- 主页 -->
  <div>
    <el-container class="home_container">
      <!-- 头部区域 -->
      <el-header>
        <img src="../assets/logo.png"
             alt="">
        <div>
          <span>系统</span>
        </div>
        <el-button type='info'
                   @click="logout">退出</el-button>
      </el-header>
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse?'63px':'200px'">
          <div class="toggle-button"
               @click="toggeleCollapse">|||</div>
          <!-- 侧边栏菜单区 -->
          <el-menu @open="handleOpen"
                   @close="handleClose"
                   background-color="#545c64"
                   text-color="#fff"
                   active-text-color="#ffd04b"
                   :unique-opened="true"
                   :collapse="isCollapse"
                   :collapse-transition="false"
                   router
                   :default-active="activePath">
            <!-- collapse-transition 取消折叠动画
            collapse侧边栏的折叠与展开
            unique-opened只允许展开一个二级菜单 -->
            <!-- 一级菜单 -->
            <el-submenu :index="item.id+''"
                        v-for="item in MenuList"
                        :key="item.id">
              <!-- 一级菜单的模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i :class="iconsObj[item.id]"></i>
                <!-- 一级菜单文本 -->
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item :index="'/'+subItem.path+''"
                            v-for="subItem in item.children"
                            :key="subItem.id"
                            @click="saveNavState('/'+subItem.path)">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                {{subItem.authName}}
              </el-menu-item>
            </el-submenu>

          </el-menu>
        </el-aside>
        <!-- 右侧主页面 -->
        <el-main>
          <router-view> </router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data () {
    return {
      MenuList: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false, // 是否折叠
      activePath: '' // 被激活的链接地址
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    handleOpen (key, keyPath) {
      console.log(key, keyPath)
    },
    handleClose (key, keyPath) {
      console.log(key, keyPath)
    },
    // 从后台获取所有的菜单
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      console.log('调用menus接口获取的菜单数据__>>', res)
      if (res.meta.status !== 200) return this.$message.err('res.meta.msg')
      this.MenuList = res.data
    },
    // 点击按钮，切换菜单的折叠与展开
    toggeleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState (activePath) {
      console.log('activePath__>', activePath)
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }

}
</script>

<style lang='less' scoped>
div {
  width: 100%;
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: aliceblue;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #545c64;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.home_container {
  height: 100%;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px; //字体大小
  line-height: 24px; //行高
  color: aliceblue;
  text-align: center;
  letter-spacing: 0.2em;
  height: 25px;
  cursor: pointer;
}
</style>
